home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_xemacs.idb / usr / freeware / lib / xemacs-20.4 / lisp / packages / compile.el.z / compile.el
Encoding:
Text File  |  1998-05-21  |  73.7 KB  |  1,959 lines

  1. ;;; compile.el --- run compiler as inferior of Emacs, parse error messages.
  2.  
  3. ;; Copyright (C) 1985, 86, 87, 93, 94, 1995, 1996 Free Software Foundation, Inc.
  4. ;; Copyright (C) 1995 Tinker Systems and INS Engineering Corp.
  5.  
  6. ;; Author: Roland McGrath <roland@prep.ai.mit.edu>
  7. ;; Maintainer: FSF
  8. ;; Keywords: tools, processes
  9.  
  10. ;; This file is part of XEmacs.
  11.  
  12. ;; XEmacs is free software; you can redistribute it and/or modify it
  13. ;; under the terms of the GNU General Public License as published by
  14. ;; the Free Software Foundation; either version 2, or (at your option)
  15. ;; any later version.
  16.  
  17. ;; XEmacs is distributed in the hope that it will be useful, but
  18. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  19. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  20. ;; General Public License for more details.
  21.  
  22. ;; You should have received a copy of the GNU General Public License
  23. ;; along with XEmacs; see the file COPYING.  If not, write to the Free
  24. ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  25. ;; 02111-1307, USA.
  26.  
  27. ;;; Synched up with: FSF 19.34, with a lot of divergence.
  28.  
  29. ;;; Commentary:
  30.  
  31. ;; This package provides the compile and grep facilities documented in
  32. ;; the Emacs user's manual.
  33.  
  34. ;;; Code:
  35.  
  36. (defgroup compilation nil
  37.   "Run compiler as inferior of Emacs, parse error messages."
  38.   :group 'programming
  39.   :group 'tools
  40.   :group 'processes)
  41.  
  42.  
  43. ;;;###autoload
  44. (defcustom compilation-mode-hook nil
  45.   "*List of hook functions run by `compilation-mode' (see `run-hooks')."
  46.   :type 'hook
  47.   :group 'compilation)
  48.  
  49. ;;;###autoload
  50. (defcustom compilation-window-height nil
  51.   "*Number of lines in a compilation window.  If nil, use Emacs default."
  52.   :type '(choice (const nil)
  53.          integer)
  54.   :group 'compilation)
  55.  
  56. ;; XEmacs change
  57. (defvar compilation-error-list 'invalid ; only valid buffer-local
  58.   "List of error message descriptors for visiting erring functions.
  59. Each error descriptor is a cons (or nil).  Its car is a marker pointing to
  60. an error message.  If its cdr is a marker, it points to the text of the
  61. line the message is about.  If its cdr is a cons, it is a list
  62. \(\(DIRECTORY . FILE\) LINE [COLUMN]\).  Or its cdr may be nil if that
  63. error is not interesting.
  64.  
  65. The value may be t instead of a list; this means that the buffer of
  66. error messages should be reparsed the next time the list of errors is wanted.
  67.  
  68. Some other commands (like `diff') use this list to control the error
  69. message tracking facilities; if you change its structure, you should make
  70. sure you also change those packages.  Perhaps it is better not to change
  71. it at all.")
  72.  
  73. (defvar compilation-old-error-list nil
  74.   "Value of `compilation-error-list' after errors were parsed.")
  75.  
  76. (defvar compilation-parse-errors-function 'compilation-parse-errors 
  77.   "Function to call to parse error messages from a compilation.
  78. It takes args LIMIT-SEARCH and FIND-AT-LEAST.
  79. If LIMIT-SEARCH is non-nil, don't bother parsing past that location.
  80. If FIND-AT-LEAST is non-nil, don't bother parsing after finding that 
  81. many new errors.
  82. It should read in the source files which have errors and set
  83. `compilation-error-list' to a list with an element for each error message
  84. found.  See that variable for more info.")
  85.  
  86. ;;;###autoload
  87. (defcustom compilation-buffer-name-function nil
  88.   "Function to compute the name of a compilation buffer.
  89. The function receives one argument, the name of the major mode of the
  90. compilation buffer.  It should return a string.
  91. nil means compute the name with `(concat \"*\" (downcase major-mode) \"*\")'."
  92.   :type 'function
  93.   :group 'compilation)
  94.  
  95. ;;;###autoload
  96. (defcustom compilation-finish-function nil
  97.   "*Function to call when a compilation process finishes.
  98. It is called with two arguments: the compilation buffer, and a string
  99. describing how the process finished."
  100.   :type 'function
  101.   :group 'compilation)
  102.  
  103. (defvar compilation-last-buffer nil
  104.   "The most recent compilation buffer.
  105. A buffer becomes most recent when its compilation is started
  106. or when it is used with \\[next-error] or \\[compile-goto-error].")
  107.  
  108. (defvar compilation-in-progress nil
  109.   "List of compilation processes now running.")
  110. (or (assq 'compilation-in-progress minor-mode-alist)
  111.     (setq minor-mode-alist (cons '(compilation-in-progress " Compiling")
  112.                  minor-mode-alist)))
  113.  
  114. ;; XEmacs change
  115. (defcustom compilation-always-signal-completion nil
  116.   "*Always give an audible signal upon compilation completion.
  117. By default that signal is only given if the bottom of the compilation
  118. buffer is not visible in its window."
  119.   :type 'boolean
  120.   :group 'compilation)
  121.  
  122. (defvar compilation-parsing-end nil
  123.   "Position of end of buffer when last error messages were parsed.")
  124.  
  125. (defvar compilation-error-message "No more errors"
  126.   "Message to print when no more matches are found.")
  127.  
  128. (defvar compilation-num-errors-found)
  129.  
  130. (defun compilation-build-compilation-error-regexp-alist ()
  131.   "Set the regular expressions used for parsing compiler
  132. errors based on the compilers listed in the variable
  133. `compilation-error-regexp-systems-list'.  Updates the
  134. variable `compilation-error-regexp-alist'."
  135.   (interactive)
  136.   (setq compilation-error-regexp-alist
  137.         (apply 'append
  138.                (mapcar
  139.                 '(lambda (elt)
  140.                    (if (or (not (consp compilation-error-regexp-systems-list))
  141.                            (and (consp (car elt))
  142.                                 (intersection (car elt)
  143.                                               compilation-error-regexp-systems-list))
  144.                            (memq (car elt) compilation-error-regexp-systems-list))
  145.                        (cdr elt)
  146.                      nil))
  147.                 compilation-error-regexp-alist-alist))))
  148.  
  149. (defvar compilation-error-regexp-alist-alist
  150.   '(
  151.     ;; NOTE!  See also grep-regexp-alist, below.
  152.  
  153.     ;; 4.3BSD grep, cc, lint pass 1:
  154.     ;;     /usr/src/foo/foo.c(8): warning: w may be used before set
  155.     ;; or GNU utilities:
  156.     ;;     foo.c:8: error message
  157.     ;; or HP-UX 7.0 fc:
  158.     ;;     foo.f          :16    some horrible error message
  159.     ;; or GNU utilities with column (GNAT 1.82):
  160.     ;;   foo.adb:2:1: Unit name does not match file name
  161.     ;; 
  162.     ;; We'll insist that the number be followed by a colon or closing
  163.     ;; paren, because otherwise this matches just about anything
  164.     ;; containing a number with spaces around it.
  165.    ((4bsd gnu)
  166.     ("\
  167. \\([a-zA-Z]?:?[^:( \t\n]+\\)[:(][ \t]*\\([0-9]+\\)\\([) \t]\\|\
  168. :\\([^0-9\n]\\|\\([0-9]+:\\)\\)\\)" 1 2 5)
  169.     )
  170.  
  171.     ;; Microsoft C/C++:
  172.     ;;  keyboard.c(537) : warning C4005: 'min' : macro redefinition
  173.     ;;  d:\tmp\test.c(23) : error C2143: syntax error : missing ';' before 'if'
  174.    (msft
  175.     ("\\(\\([a-zA-Z]:\\)?[^:( \t\n-]+\\)[:(][ \t]*\\([0-9]+\\)[:) \t]" 1 3)
  176.     )
  177.  
  178.     ;; Borland C++:
  179.     ;;  Error ping.c 15: Unable to open include file 'sys/types.h'
  180.     ;;  Warning ping.c 68: Call to function 'func' with no prototype
  181.    (borland
  182.     ("\\(Error\\|Warning\\) \\([a-zA-Z]?:?[^:( \t\n]+\\)\
  183.  \\([0-9]+\\)\\([) \t]\\|:[^0-9\n]\\)" 2 3)
  184.     )
  185.  
  186.     ;; 4.3BSD lint pass 2
  187.     ;;     strcmp: variable # of args. llib-lc(359)  ::  /usr/src/foo/foo.c(8)
  188.    (4bsd
  189.     ("[^\n]*[ \t:]\\([a-zA-Z]?:?[^:( \t\n]+\\)[:(](+[ \t]*\\([0-9]+\\))[:) \t]*$"
  190.      1 2)
  191.     )
  192.  
  193.     ;; 4.3BSD lint pass 3
  194.     ;;     bloofle defined( /users/wolfgang/foo.c(4) ), but never used
  195.     ;; This used to be
  196.     ;; ("[ \t(]+\\([a-zA-Z]?:?[^:( \t\n]+\\)[:( \t]+\\([0-9]+\\)[:) \t]+" 1 2)
  197.     ;; which is regexp Impressionism - it matches almost anything!
  198.    (4bsd
  199.     ("[^\n]*([ \t]*\\([a-zA-Z]?:?[^:( \t\n]+\\)[:(][ \t]*\\([0-9]+\\))" 1 2)
  200.     )
  201.  
  202.     ;; MIPS lint pass<n>; looks good for SunPro lint also
  203.     ;;  TrimMask (255) in solomon.c may be indistinguishable from TrimMasks (93) in solomon.c due to truncation
  204.    (mips
  205.     ("[^ \n]+ (\\([0-9]+\\)) in \\([^ \n]+\\)" 2 1)
  206.     ;;  name defined but never used: LinInt in cmap_calc.c(199)
  207.     ("[^\n]*in \\([^(\n]+\\)(\\([0-9]+\\))$" 1 2)
  208.     )
  209.  
  210.     ;; Ultrix 3.0 f77:
  211.     ;;  fort: Severe: addstf.f, line 82: Missing operator or delimiter symbol
  212.     ;; Some SGI cc version:
  213.     ;;  cfe: Warning 835: foo.c, line 2: something
  214.    ((sgi ultrix)
  215.     ("\\(cfe\\|fort\\): [^:\n]*: \\([^ \n]*\\), line \\([0-9]+\\):" 2 3)
  216.     )
  217.     ;;  Error on line 3 of t.f: Execution error unclassifiable statement    
  218.     ;; Unknown who does this:
  219.     ;;  Line 45 of "foo.c": bloofle undefined
  220.     ;; Absoft FORTRAN 77 Compiler 3.1.3
  221.     ;;  error on line 19 of fplot.f: spelling error?
  222.     ;;  warning on line 17 of fplot.f: data type is undefined for variable d
  223.    (of
  224.     ("\\(\\|[^\n]* on \\)[Ll]ine[ \t]+\\([0-9]+\\)[ \t]+\
  225. of[ \t]+\"?\\([a-zA-Z]?:?[^\":\n]+\\)\"?:" 3 2)
  226.     )
  227.  
  228.     ;; Apollo cc, 4.3BSD fc:
  229.     ;;    "foo.f", line 3: Error: syntax error near end of statement
  230.     ;; IBM RS6000:
  231.     ;;  "vvouch.c", line 19.5: 1506-046 (S) Syntax error.
  232.     ;; Unknown compiler:
  233.     ;;  File "foobar.ml", lines 5-8, characters 20-155: blah blah
  234.     ;; Microtec mcc68k:
  235.     ;;  "foo.c", line 32 pos 1; (E) syntax error; unexpected symbol: "lossage"
  236.     ;; GNAT (as of July 94): 
  237.     ;;  "foo.adb", line 2(11): warning: file name does not match ...
  238.     ;; IBM AIX xlc compiler:
  239.     ;;  "src/swapping.c", line 30.34: 1506-342 (W) "/*" detected in comment.
  240.    (comma
  241.     ("[^\n]*\"\\([^,\" \n\t]+\\)\", lines? \
  242. \\([0-9]+\\)\\([\(.]\\([0-9]+\\)\)?\\)?[:., (-]" 1 2 4)
  243.     )
  244.  
  245.     ;; MIPS RISC CC - the one distributed with Ultrix:
  246.     ;;    ccom: Error: foo.c, line 2: syntax error
  247.     ;; DEC AXP OSF/1 cc
  248.     ;;  /usr/lib/cmplrs/cc/cfe: Error: foo.c: 1: blah blah 
  249.    ((mips ultrix)
  250.     ("[^\n]*rror: \\([^,\" \n\t]+\\)[,:] \\(line \\)?\\([0-9]+\\):" 1 3)
  251.     )
  252.  
  253.     ;; IBM AIX PS/2 C version 1.1:
  254.     ;;    ****** Error number 140 in line 8 of file errors.c ******
  255.    (aix
  256.     ("[^\n]*in line \\([0-9]+\\) of file \\([^ \n]+[^. \n]\\)\\.? " 2 1)
  257.     )
  258.     ;; IBM AIX lint is too painful to do right this way.  File name
  259.     ;; prefixes entire sections rather than being on each line.
  260.  
  261.     ;; Lucid Compiler, lcc 3.x
  262.     ;; E, file.cc(35,52) Illegal operation on pointers
  263.    (lcc
  264.     ("[EW], \\([^(\n]*\\)(\\([0-9]+\\),[ \t]*\\([0-9]+\\)" 1 2 3)
  265.     )
  266.  
  267.     ;; GNU messages with program name and optional column number.
  268.    (gnu
  269.     ("[a-zA-Z]?:?[^0-9 \n\t:]+:[ \t]*\\([^ \n\t:]+\\):\
  270. \\([0-9]+\\):\\(\\([0-9]+\\)[: \t]\\)?" 1 2 4)
  271.     )
  272.  
  273.     ;; GNU messages with program name and optional column number
  274.     ;; and a severity letter after that.  nsgmls makes them.
  275.    (gnu
  276.     ("[^0-9 \n\t:]+:[ \t]*\\([^ \n\t:]+\\):\
  277. \\([0-9]+\\):\\(\\([0-9]+\\):\\)?[A-Za-z]:" 1 2 4)
  278.     )
  279.  
  280.     ;; jwz:
  281.     ;; IRIX 5.2
  282.     ;; cfe: Warning 712: foo.c, line 2: illegal combination of pointer and ...
  283.    (sgi
  284.     ("[^\n]* \\([^ \n,\"]+\\), line \\([0-9]+\\):" 1 2)
  285.     )
  286.     ;; IRIX 5.2
  287.     ;; cfe: Warning 600: xfe.c: 170: Not in a conditional directive while ...
  288.    (sgi
  289.     ("[^\n]*: \\([^ \n,\"]+\\): \\([0-9]+\\):" 1 2)
  290.     )
  291.  
  292.     ;; Cray C compiler error messages
  293.    (cray
  294.     ("\\(cc\\| cft\\)-[0-9]+ c\\(c\\|f77\\): ERROR \\([^,\n]+, \\)* File = \\([^,\n]+\\), Line = \\([0-9]+\\)" 4 5)
  295.     )
  296.  
  297.     ;; IBM C/C++ Tools 2.01:
  298.     ;;  foo.c(2:0) : informational EDC0804: Function foo is not referenced.
  299.     ;;  foo.c(3:8) : warning EDC0833: Implicit return statement encountered.
  300.     ;;  foo.c(5:5) : error EDC0350: Syntax error.
  301.    (ibm
  302.     ("\\([^( \n\t]+\\)(\\([0-9]+\\):\\([0-9]+\\)) : " 1 2 3)
  303.     )
  304.  
  305.     ;; Sun ada (VADS, Solaris):
  306.     ;;  /home3/xdhar/rcds_rc/main.a, line 361, char 6:syntax error: "," inserted
  307.    (ada
  308.     ("\\([^, \n\t]+\\), line \\([0-9]+\\), char \\([0-9]+\\)[:., \(-]" 1 2 3)
  309.     )
  310.    )
  311.   "Alist of (system regexp-alist) for building
  312. `compilation-error-regexp-alist'.  SYSTEM is either a system identifier,
  313. or a list of system identifiers.  See the variable 
  314. `compilation-error-regexp-systems-list'")
  315.  
  316. (defvar compilation-error-regexp-alist nil
  317.  "Alist that specifies how to match errors in compiler output.
  318. Each elt has the form (REGEXP FILE-IDX LINE-IDX [COLUMN-IDX FILE-FORMAT...])
  319. If REGEXP matches constrained to the beginning of the line, the
  320. FILE-IDX'th subexpression gives the file name, and the LINE-IDX'th
  321. subexpression gives the line number.  If COLUMN-IDX is given, the
  322. COLUMN-IDX'th subexpression gives the column number on that line.  If
  323. any FILE-FORMAT is given, each is a format string to produce a file name
  324. to try; %s in the string is replaced by the text matching the
  325. FILE-IDX'th subexpression.  Note previously REGEXP was not constrained
  326. to the beginning of the line, so old patterns without leading `^' or `\\n'
  327. may now require a leading `.*'.
  328.  
  329. Note that this now gets set by the function 
  330. `compilation-build-compilation-error-regexp-alist' using the 
  331. value of the variable `compilation-error-regexp-alist-alist'")
  332.  
  333. (defcustom compilation-error-regexp-systems-list 'all
  334.   "*This is either the symbol `all', or a list of systems for which
  335. compilation error regexps should be included in
  336. `compilation-error-regexp-alist'.  You must run the function
  337. `compilation-build-compilation-error-regexp-alist' after changing
  338. the value of this variable for the change to take effect.
  339.  
  340. The list of known systems is:
  341.   gnu:      but of course
  342.   lcc:      Lucid compilers
  343.   ada:      Ada compilers
  344.   of:       Using tool that says line xx of foo.c
  345.   comma:    Using tool that says \"foo.c\", line 12
  346.   4bsd:     Using 4bsd
  347.   msft:     Using microsoft
  348.   borland:  Using Borland
  349.   mips:     Using Mips
  350.   sgi:      Using SGI
  351.   cray:     Using Cray
  352.   ibm:      IBM C compilers
  353.   aix:      the operating system
  354.   ultrix:   the operating system
  355.  
  356. See also the variable `compilation-error-regexp-alist-alist'."
  357.   :type '(choice (const all)
  358.          (set :menu-tag "Pick"
  359.               (const gnu) (const lcc) (const ada)
  360.               (const of) (const comma) (const 4bsd)
  361.               (const msft) (const borland) (const mips)
  362.               (const sgi) (const cray) (const ibm)
  363.               (const aix) (const ultrix)))
  364.   :set (lambda (symbol value)
  365.          (set-default symbol value)
  366.          (compilation-build-compilation-error-regexp-alist))
  367.   :group 'compilation)
  368.  
  369. (compilation-build-compilation-error-regexp-alist)
  370.  
  371. (defcustom compilation-read-command t
  372.   "*If not nil, M-x compile reads the compilation command to use.
  373. Otherwise, M-x compile just uses the value of `compile-command'."
  374.   :type 'boolean
  375.   :group 'compilation)
  376.  
  377. (defcustom compilation-ask-about-save t
  378.   "*If not nil, M-x compile asks which buffers to save before compiling.
  379. Otherwise, it saves all modified buffers without asking."
  380.   :type 'boolean
  381.   :group 'compilation)
  382.  
  383. (defvar grep-regexp-alist
  384.   '(("^\\([a-zA-Z]?:?[^:( \t\n]+\\)[:( \t]+\\([0-9]+\\)[:) \t]" 1 2))
  385.   "Regexp used to match grep hits.  See `compilation-error-regexp-alist'.")
  386.  
  387. (defcustom grep-command "grep -n "
  388.   "*Last grep command used in \\[grep]; default for next grep."
  389.   :type 'string
  390.   :group 'compilation)
  391.  
  392. ;;;###autoload
  393. (defcustom compilation-search-path '(nil)
  394.   "*List of directories to search for source files named in error messages.
  395. Elements should be directory names, not file names of directories.
  396. nil as an element means to try the default directory."
  397.   :type '(repeat (choice (const :tag "Default" nil)
  398.              directory))
  399.   :group 'compilation)
  400.  
  401. (defcustom compile-command "make -k "
  402.   "*Last shell command used to do a compilation; default for next compilation.
  403.  
  404. Sometimes it is useful for files to supply local values for this variable.
  405. You might also use mode hooks to specify it in certain modes, like this:
  406.  
  407.     (setq c-mode-hook
  408.       '(lambda () (or (file-exists-p \"makefile\") (file-exists-p \"Makefile\")
  409.               (progn (make-local-variable 'compile-command)
  410.                  (setq compile-command
  411.                     (concat \"make -k \"
  412.                         buffer-file-name))))))"
  413.   :type 'string
  414.   :group 'compilation)
  415.  
  416. (defvar compilation-enter-directory-regexp
  417.   "[^\n]*: Entering directory `\\([^\n]*\\)'$"
  418.   "Regular expression matching lines that indicate a new current directory.
  419. This must contain one \\(, \\) pair around the directory name.
  420.  
  421. The default value matches lines printed by the `-w' option of GNU Make.")
  422.  
  423. (defvar compilation-leave-directory-regexp
  424.   "[^\n]*: Leaving directory `\\([^\n]*\\)'$"
  425.   "Regular expression matching lines that indicate restoring current directory.
  426. This may contain one \\(, \\) pair around the name of the directory
  427. being moved from.  If it does not, the last directory entered \(by a
  428. line matching `compilation-enter-directory-regexp'\) is assumed.
  429.  
  430. The default value matches lines printed by the `-w' option of GNU Make.")
  431.  
  432. (defvar compilation-directory-stack nil
  433.   "Stack of previous directories for `compilation-leave-directory-regexp'.
  434. The head element is the directory the compilation was started in.")
  435.  
  436. (defvar compilation-exit-message-function nil "\
  437. If non-nil, called when a compilation process dies to return a status message.
  438. This should be a function of three arguments: process status, exit status,
  439. and exit message; it returns a cons (MESSAGE . MODELINE) of the strings to
  440. write into the compilation buffer, and to put in its mode line.")
  441.  
  442. ;; History of compile commands.
  443. (defvar compile-history nil)
  444. ;; History of grep commands.
  445. (defvar grep-history nil)
  446.  
  447. ;; XEmacs
  448. (defvar compilation-font-lock-keywords (purecopy
  449.   (list
  450.    '("^[-_.\"A-Za-z0-9/+]+\\(:\\|, line \\)[0-9]+: \\([wW]arning:\\).*$" .
  451.      font-lock-keyword-face)
  452.    '("^[-_.\"A-Za-z0-9/+]+\\(: *\\|, line \\)[0-9]+:.*$" . font-lock-function-name-face)
  453.    '("^[^:\n]+-[a-zA-Z][^:\n]+$" . font-lock-doc-string-face)
  454.    '("\\(^[-_.\"A-Za-z0-9/+]+\\)\\(: *\\|, line \\)[0-9]+" 1 font-lock-string-face t)
  455.    '("^[-_.\"A-Za-z0-9/+]+\\(: *[0-9]+\\|, line [0-9]+\\)" 1 bold t)
  456.    ))
  457.   "Additional expressions to highlight in Compilation mode.")
  458.  
  459. ;FSF's version.  Ours looks better.
  460. ;(defvar compilation-mode-font-lock-keywords
  461. ;  ;; This regexp needs a bit of rewriting.  What is the third grouping for?
  462. ;  '(("^\\([a-zA-Z]?:?[^ \n:]*:\\([0-9]+:\\)+\\)\\(.*\\)$"
  463. ;     1 font-lock-function-name-face))
  464. ;;;  ("^\\([^\n:]*:\\([0-9]+:\\)+\\)\\(.*\\)$" 0 font-lock-keyword-face keep)
  465.  
  466. ;; XEmacs change
  467. (put 'compilation-mode 'font-lock-defaults
  468.      '(compilation-font-lock-keywords t))
  469.  
  470. (defcustom compilation-mouse-motion-initiate-parsing t
  471.   "*Should mouse motion over the compilation buffer initiate parsing?
  472. When set to a non-nil value, mouse motion over the compilation/grep
  473. buffer may initiate parsing of the error messages or grep hits.
  474. When this is nil, errors and grep matches will no longer be 
  475. highlighted until they have been parsed, but may still be selected
  476. with the center mouse button.  This will then initiate parsing
  477. and jump to the corresponding source line."
  478.   :type 'boolean
  479.   :group 'compilation)
  480.  
  481. ;;;###autoload
  482. (defun compile (command)
  483.   "Compile the program including the current buffer.  Default: run `make'.
  484. Runs COMMAND, a shell command, in a separate process asynchronously
  485. with output going to the buffer `*compilation*'.
  486.  
  487. You can then use the command \\[next-error] to find the next error message
  488. and move to the source code that caused it.
  489.  
  490. Interactively, prompts for the command if `compilation-read-command' is
  491. non-nil; otherwise uses `compile-command'.  With prefix arg, always prompts.
  492.  
  493. To run more than one compilation at once, start one and rename the
  494. \`*compilation*' buffer to some other name with \\[rename-buffer].
  495. Then start the next one.
  496.  
  497. The name used for the buffer is actually whatever is returned by
  498. the function in `compilation-buffer-name-function', so you can set that
  499. to a function that generates a unique name."
  500.   (interactive 
  501.    (if (or compilation-read-command current-prefix-arg)
  502.        ;; XEmacs change
  503.        (list (read-shell-command "Compile command: "
  504.                                  compile-command
  505.                                  ;; #### minibuffer code should do this
  506.                                  (if (equal (car compile-history)
  507.                                             compile-command)
  508.                                      '(compile-history . 1)
  509.                                      'compile-history)))
  510.        (list compile-command)))
  511.   (setq compile-command command)
  512.   (save-some-buffers (not compilation-ask-about-save) nil)
  513.   (compile-internal compile-command "No more errors"))
  514.  
  515. ;;; run compile with the default command line
  516. (defun recompile ()
  517.   "Re-compile the program including the current buffer."
  518.   (interactive)
  519.   (save-some-buffers (not compilation-ask-about-save) nil)
  520.   (compile-internal compile-command "No more errors"))
  521.  
  522. ;; The system null device. (Should reference NULL_DEVICE from C.)
  523. (defvar grep-null-device "/dev/null" "The system null device.")
  524.  
  525. ;;;###autoload
  526. (defun grep (command-args)
  527.   "Run grep, with user-specified args, and collect output in a buffer.
  528. While grep runs asynchronously, you can use the \\[next-error] command
  529. to find the text that grep hits refer to.
  530.  
  531. This command uses a special history list for its arguments, so you can
  532. easily repeat a grep command."
  533.   (interactive
  534.    ;; XEmacs change
  535.    (list (read-shell-command "Run grep (like this): "
  536.                  grep-command 'grep-history)))
  537.   (let ((buf (compile-internal (concat command-args " " grep-null-device)
  538.                    "No more grep hits" "grep"
  539.                    ;; Give it a simpler regexp to match.
  540.                    nil grep-regexp-alist)))
  541.     (save-excursion
  542.       (set-buffer buf)
  543.       (set (make-local-variable 'compilation-exit-message-function)
  544.        ;; XEmacs change
  545.        (lambda (proc msg)
  546.          (let ((code (process-exit-status proc)))
  547.            (if (eq (process-status proc) 'exit)
  548.            (cond ((zerop code)
  549.               '("finished (matches found)\n" . "matched"))
  550.              ((= code 1)
  551.               '("finished with no matches found\n" . "no match"))
  552.              (t
  553.               (cons msg code)))
  554.          (cons msg code))))))))
  555.  
  556. (defun compile-internal (command error-message
  557.                  &optional name-of-mode parser regexp-alist
  558.                  name-function)
  559.   "Run compilation command COMMAND (low level interface).
  560. ERROR-MESSAGE is a string to print if the user asks to see another error
  561. and there are no more errors.  Third argument NAME-OF-MODE is the name
  562. to display as the major mode in the compilation buffer.
  563.  
  564. Fourth arg PARSER is the error parser function (nil means the default).  Fifth
  565. arg REGEXP-ALIST is the error message regexp alist to use (nil means the
  566. default).  Sixth arg NAME-FUNCTION is a function called to name the buffer (nil
  567. means the default).  The defaults for these variables are the global values of
  568. \`compilation-parse-errors-function', `compilation-error-regexp-alist', and
  569. \`compilation-buffer-name-function', respectively.
  570.  
  571. Returns the compilation buffer created."
  572.   (let (outbuf)
  573.     (save-excursion
  574.       (or name-of-mode
  575.       (setq name-of-mode "Compilation"))
  576.       (setq outbuf
  577.         (get-buffer-create
  578.          (funcall (or name-function compilation-buffer-name-function
  579.               (function (lambda (mode)
  580.                       (concat "*" (downcase mode) "*"))))
  581.               name-of-mode)))
  582.       (set-buffer outbuf)
  583.       (let ((comp-proc (get-buffer-process (current-buffer))))
  584.     (if comp-proc
  585.         (if (or (not (eq (process-status comp-proc) 'run))
  586.             (yes-or-no-p
  587.              (format "A %s process is running; kill it? "
  588.                  name-of-mode)))
  589.         (condition-case ()
  590.             (progn
  591.               (interrupt-process comp-proc)
  592.               (sit-for 1)
  593.               (delete-process comp-proc))
  594.           (error nil))
  595.           (error "Cannot have two processes in `%s' at once"
  596.              (buffer-name))
  597.           )))
  598.       ;; In case the compilation buffer is current, make sure we get the global
  599.       ;; values of compilation-error-regexp-alist, etc.
  600.       (kill-all-local-variables))
  601.     (let ((regexp-alist (or regexp-alist compilation-error-regexp-alist))
  602.       (parser (or parser compilation-parse-errors-function))
  603.       (thisdir default-directory)
  604.       ;; XEmacs change
  605.       (buffer-save (current-buffer))
  606.       outwin)
  607.  
  608.       ;; XEmacs change
  609.       ;; Pop up the compilation buffer.
  610.       (setq outwin (display-buffer outbuf))
  611.       
  612.       (unwind-protect
  613.        (progn
  614.     ;; Clear out the compilation buffer and make it writable.
  615.     ;; Change its default-directory to the directory where the compilation
  616.     ;; will happen, and insert a `cd' command to indicate this.
  617.     (set-buffer outbuf)
  618.  
  619.     (setq buffer-read-only nil)
  620.     (buffer-disable-undo (current-buffer))
  621.     (erase-buffer)
  622.     (buffer-enable-undo (current-buffer))
  623.     (setq default-directory thisdir)
  624.     (insert "cd " thisdir "\n" command "\n")
  625.     (set-buffer-modified-p nil)
  626.  
  627.     ;; XEmacs change
  628.     ;; set it so the window will scroll to show compile output
  629.     (save-window-excursion
  630.       (select-window outwin)
  631.       (goto-char (point-max)))
  632.     
  633.     ;; XEmacs change
  634.     (compilation-mode name-of-mode)
  635.     ;; (setq buffer-read-only t)  ;;; Non-ergonomic.
  636.     ;; XEmacs change
  637.     (set (make-local-variable 'compile-command) command)
  638.     (set (make-local-variable 'compilation-parse-errors-function) parser)
  639.     (set (make-local-variable 'compilation-error-message) error-message)
  640.     (set (make-local-variable 'compilation-error-regexp-alist) regexp-alist)
  641.     (setq default-directory thisdir
  642.           compilation-directory-stack (list default-directory))
  643.     (set-window-start outwin (point-min))
  644.     (setq mode-name name-of-mode)
  645.     ;; XEmacs change
  646. ;    (or (eq outwin (selected-window))
  647. ;        (set-window-point outwin (point-min)))
  648.     (compilation-set-window-height outwin)
  649.     ;; Start the compilation.
  650.     (if (fboundp 'start-process)
  651.         (let* ((process-environment (cons "EMACS=t" process-environment))
  652.            (proc (start-process-shell-command (downcase mode-name)
  653.                               outbuf
  654.                               command)))
  655.           (set-process-sentinel proc 'compilation-sentinel)
  656.           (set-process-filter proc 'compilation-filter)
  657.           (set-marker (process-mark proc) (point) outbuf)
  658.           (setq compilation-in-progress 
  659.             (cons proc compilation-in-progress)))
  660.       ;; No asynchronous processes available.
  661.       (display-message
  662.        'progress
  663.        (format "Executing `%s'..." command))
  664.       ; FSF
  665.       ; (setq mode-line-process ":run")
  666.       ; (force-mode-line-update)
  667.       (sit-for 0)            ;; Force redisplay
  668.       (let ((status (call-process shell-file-name nil outbuf nil "-c"
  669.                       command))))
  670.       (display-message
  671.        'progress
  672.        (format "Executing `%s'...done" command))))
  673.        (set-buffer buffer-save)))
  674.  
  675.     ;; Make it so the next C-x ` will use this buffer.
  676.     (setq compilation-last-buffer outbuf)))
  677.  
  678. ;; Set the height of WINDOW according to compilation-window-height.
  679. (defun compilation-set-window-height (window)
  680.   (and compilation-window-height
  681.        (= (window-width window) (frame-width (window-frame window)))
  682.        ;; If window is alone in its frame, aside from a minibuffer,
  683.        ;; don't change its height.
  684.        (not (eq window (frame-root-window (window-frame window))))
  685.        ;; This save-excursion prevents us from changing the current buffer,
  686.        ;; which might not be the same as the selected window's buffer.
  687.        (save-excursion
  688.      (let ((w (selected-window)))
  689.        (unwind-protect
  690.            (progn
  691.          (select-window window)
  692.          (enlarge-window (- compilation-window-height
  693.                     (window-height))))
  694.          (select-window w))))))
  695.  
  696. (defvar compilation-minor-mode-map
  697.   (let ((map (make-sparse-keymap)))
  698.     (set-keymap-name map 'compilation-minor-mode-map)
  699.     (define-key map "\C-c\C-c" 'compile-goto-error)
  700.     (define-key map "\C-m" 'compile-goto-error)
  701.     (define-key map "\C-c\C-k" 'kill-compilation)
  702.     (define-key map "\M-n" 'compilation-next-error)
  703.     (define-key map "\M-p" 'compilation-previous-error)
  704.     (define-key map "\M-{" 'compilation-previous-file)
  705.     (define-key map "\M-}" 'compilation-next-file)
  706.     map)
  707.   "Keymap for `compilation-minor-mode'.")
  708.  
  709. (defvar compilation-mode-map
  710.   (let ((map (make-sparse-keymap)))
  711.     (set-keymap-parents map (list compilation-minor-mode-map))
  712.     (set-keymap-name map 'compilation-mode-map)
  713.     (define-key map " " 'scroll-up)
  714.     (define-key map "\^?" 'scroll-down)
  715.     (define-key map 'button2 'compile-mouse-goto-error)
  716.     map)
  717.   "Keymap for compilation log buffers.
  718. `compilation-minor-mode-map' is a parent of this.")
  719.  
  720. ;;; XEmacs menus
  721.  
  722. (defun compilation-errors-exist-p (&optional buffer)
  723.   "Whether we are in a state where the `next-error' command will work,
  724. that is, whether there exist (or may exist) error targets in the *compile*
  725. or *grep* buffers."
  726.   (or buffer
  727.       (setq buffer (condition-case nil
  728.               (compilation-find-buffer)
  729.             (error nil))))
  730.   (and buffer
  731.        (compilation-buffer-p buffer)
  732.        (save-excursion
  733.      (set-buffer buffer)
  734.      ;; Has errors on the list, or needs to be parsed.
  735.      ;; But don't parse it now!
  736.      (or (not (null compilation-error-list))
  737.          (< compilation-parsing-end (point-max))))))
  738.  
  739. (defvar Compilation-mode-popup-menu
  740.   '("Compilation Mode Commands"
  741.     :filter compile-menu-filter
  742.     ["Compile..."    compile t]
  743.     ["Recompile"    recompile t]
  744.     ["Kill Compilation"    kill-compilation (get-buffer-process (current-buffer))]
  745.     "---"
  746.     ["Goto Error"    compile-goto-error    (compilation-errors-exist-p)]
  747.     ["Next Error"     next-error        (compilation-errors-exist-p)]
  748.     ["Previous Error"    previous-error        (compilation-errors-exist-p)]
  749.     ["First Error"    first-error        (compilation-errors-exist-p)]
  750.     ))
  751.  
  752. (defvar Compilation-mode-menubar-menu
  753.   (cons "Compile" (cdr Compilation-mode-popup-menu)))
  754.   
  755. (defvar grep-mode-popup-menu
  756.   '("Grep Mode Commands"
  757.     :filter grep-menu-filter
  758.     ["Grep..."        grep t]
  759.     ["Repeat Grep"    recompile t]
  760.     ["Kill Grep"    kill-compilation (get-buffer-process (current-buffer))]
  761.     "---"
  762.     ["Goto Match" compile-goto-error (default-value 'compilation-error-list)]
  763.     ["Next Match"    next-error (default-value 'compilation-error-list)]
  764.     ["Previous Match"    previous-error (default-value 'compilation-error-list)]
  765.     ["First Match"    first-error (default-value 'compilation-error-list)]
  766.     ))
  767.  
  768. (defvar grep-mode-menubar-menu
  769.   (cons "Grep" (cdr grep-mode-popup-menu)))
  770.   
  771. (defun compile-menu-filter-1 (menu history-list item-name command-name)
  772.   (let ((submenu (mapcar #'(lambda (string)
  773.                  (vector string
  774.                      (list command-name string)
  775.                      t))
  776.               history-list))
  777.     (existing (assoc item-name menu)))
  778.     (if existing
  779.     (progn
  780.       (setcdr existing submenu)
  781.       menu)
  782.       (nconc menu (list (cons item-name submenu))))))
  783.  
  784. (defun compile-menu-filter (menu)
  785.   (compile-menu-filter-1 menu compile-history "Compile History" 'compile))
  786.  
  787. (defun grep-menu-filter (menu)
  788.   (compile-menu-filter-1 menu grep-history "Grep History" 'grep))
  789.  
  790. ;;;###autoload
  791. (defun compilation-mode (&optional name-of-mode)
  792.   "Major mode for compilation log buffers.
  793. \\<compilation-mode-map>To visit the source for a line-numbered error,
  794. move point to the error message line and type \\[compile-goto-error],
  795. or click on the line with \\[compile-mouse-goto-error].
  796. There is a menu of commands on \\[compile-popup-menu].
  797. To kill the compilation, type \\[kill-compilation].
  798.  
  799. Runs `compilation-mode-hook' with `run-hooks' (which see)."
  800.   (interactive)
  801.   (kill-all-local-variables)
  802.   (use-local-map compilation-mode-map)
  803.   (setq major-mode 'compilation-mode
  804.     mode-name "Compilation")
  805.   (compilation-setup)
  806.   (font-lock-set-defaults)
  807.   (if (not name-of-mode) nil
  808.     (let ((sym (intern (concat name-of-mode "-mode-popup-menu"))))
  809.       (if (boundp sym)
  810.       (setq mode-popup-menu (symbol-value sym))))
  811.     (if (featurep 'menubar)
  812.     (progn
  813.       ;; make a local copy of the menubar, so our modes don't
  814.       ;; change the global menubar
  815.       (set-buffer-menubar current-menubar)
  816.       (let ((sym (intern (concat name-of-mode "-mode-menubar-menu"))))
  817.         (if (boundp sym)
  818.         (add-submenu nil (symbol-value sym)))))))
  819.   (run-hooks 'compilation-mode-hook))
  820.  
  821. ;; XEmacs addition, hacked by Mly
  822. (defun compilation-mode-motion-hook (event)
  823.   (mode-motion-highlight-internal
  824.     event
  825.     #'beginning-of-line
  826.     #'(lambda ()
  827.         (let* ((p (point))
  828.                (e (progn (end-of-line) (point)))
  829.                (l (progn
  830.                     (if (and compilation-mouse-motion-initiate-parsing
  831.                              (or (eq compilation-error-list 't)
  832.                                  (>= p compilation-parsing-end)))
  833.                         ;; #### Does it suck too badly to have mouse-movement
  834.                         ;; #### over a buffer parse errors in that buffer??
  835.                         (save-window-excursion
  836.                           (compile-reinitialize-errors nil p)))
  837.                     (if (and compilation-error-list
  838.                              (<= (car (car compilation-error-list)) p))
  839.                         ;; Perhaps save time by only searching tail
  840.                         compilation-error-list
  841.                         compilation-old-error-list))))
  842.           (if (catch 'found
  843.                 (while l
  844.                   (let ((x (marker-position (car (car l)))))
  845.                     (cond ((< x p)
  846.                            (setq l (cdr l)))
  847.                           ((<= x e)
  848.                            (throw 'found t))
  849.                           (t
  850.                            (throw 'found nil)))))
  851.                 nil)
  852.               (goto-char e)
  853.               (goto-char p))))))
  854.  
  855. ;; Prepare the buffer for the compilation parsing commands to work.
  856. (defun compilation-setup ()
  857.   ;; Make the buffer's mode line show process state.
  858.   (setq mode-line-process '(":%s"))
  859.   (set (make-local-variable 'compilation-error-list) nil)
  860.   (set (make-local-variable 'compilation-old-error-list) nil)
  861.   (set (make-local-variable 'compilation-parsing-end) 1)
  862.   (set (make-local-variable 'compilation-directory-stack) nil)
  863.   (setq compilation-last-buffer (current-buffer))
  864.   ;; XEmacs change: highlight lines, install menubar.
  865.   (require 'mode-motion)
  866.   (setq mode-motion-hook 'compilation-mode-motion-hook)
  867.   (make-local-variable 'mouse-track-click-hook)
  868.   (add-hook 'mouse-track-click-hook 'compile-mouse-maybe-goto-error)
  869.   )
  870.  
  871. (defvar compilation-minor-mode nil
  872.   "Non-nil when in compilation-minor-mode.
  873. In this minor mode, all the error-parsing commands of the
  874. Compilation major mode are available.")
  875. (make-variable-buffer-local 'compilation-minor-mode)
  876.  
  877. (or (assq 'compilation-minor-mode minor-mode-alist)
  878.     (setq minor-mode-alist (cons '(compilation-minor-mode " Compilation")
  879.                  minor-mode-alist)))
  880. (or (assq 'compilation-minor-mode minor-mode-map-alist)
  881.     (setq minor-mode-map-alist (cons (cons 'compilation-minor-mode
  882.                        compilation-minor-mode-map)
  883.                      minor-mode-map-alist)))
  884.  
  885. ;;;###autoload
  886. (defun compilation-minor-mode (&optional arg)
  887.   "Toggle compilation minor mode.
  888. With arg, turn compilation mode on if and only if arg is positive.
  889. See `compilation-mode'.
  890. ! \\{compilation-mode-map}"
  891.   (interactive "P")
  892.   (if (setq compilation-minor-mode (if (null arg)
  893.                        (null compilation-minor-mode)
  894.                      (> (prefix-numeric-value arg) 0)))
  895.       (progn
  896.     (compilation-setup)
  897.     (run-hooks 'compilation-minor-mode-hook))))
  898.  
  899. ;; Write msg in the current buffer and hack its mode-line-process.
  900. (defun compilation-handle-exit (process-status exit-status msg)
  901.   (let ((buffer-read-only nil)
  902.     (status (if compilation-exit-message-function
  903.             (funcall compilation-exit-message-function
  904.                  process-status exit-status msg)
  905.           (cons msg exit-status)))
  906.     (omax (point-max))
  907.     (opoint (point)))
  908.     ;; Record where we put the message, so we can ignore it
  909.     ;; later on.
  910.     (goto-char omax)
  911.     (insert ?\n mode-name " " (car status))
  912.     (forward-char -1)
  913.     (insert " at " (substring (current-time-string) 0 19))
  914.     (forward-char 1)
  915.     (setq mode-line-process (format ":%s [%s]" process-status (cdr status)))
  916.     ;; Force mode line redisplay soon.
  917.     (force-mode-line-update)
  918.     (if (and opoint (< opoint omax))
  919.     (goto-char opoint))
  920.     (if compilation-finish-function
  921.     (funcall compilation-finish-function (current-buffer) msg))))
  922.  
  923. ;; Called when compilation process changes state.
  924. (defun compilation-sentinel (proc msg)
  925.   "Sentinel for compilation buffers."
  926.   ;; XEmacs change
  927.   (let* ((buffer (process-buffer proc))
  928.      (window (get-buffer-window buffer)))
  929.     (if (memq (process-status proc) '(signal exit))
  930.     (progn
  931.       (if (null (buffer-name buffer))
  932.           ;; buffer killed
  933.           (set-process-buffer proc nil)
  934.         (let ((obuf (current-buffer))
  935.           omax opoint estatus)
  936.           ;; save-excursion isn't the right thing if
  937.           ;; process-buffer is current-buffer
  938.           (unwind-protect
  939.           (progn
  940.             ;; Write something in the compilation buffer
  941.             ;; and hack its mode line.
  942.             (set-buffer buffer)
  943.             (let ((buffer-read-only nil)
  944.               (status (if compilation-exit-message-function
  945.                       (funcall compilation-exit-message-function
  946.                            proc msg)
  947.                     (cons msg (process-exit-status proc)))))
  948.               (setq omax (point-max)
  949.                 opoint (point))
  950.               (goto-char omax)
  951.               ;; Record where we put the message, so we can ignore it
  952.               ;; later on.
  953.               (insert ?\n mode-name " " (car status))
  954.               (forward-char -1)
  955.               (insert " at " (substring (current-time-string) 0 19))
  956.               (forward-char 1)
  957.               (setq mode-line-process
  958.                 (concat ":"
  959.                     (symbol-name (process-status proc))
  960.                     (if (zerop (process-exit-status proc))
  961.                     " OK"
  962.                     (setq estatus
  963.                           (format " [exit-status %d]"
  964.                               (process-exit-status proc))))
  965.                     ))
  966.               ;; XEmacs - tedium should let you know when it's ended...
  967.               (if (and (not compilation-always-signal-completion)
  968.                    window
  969.                    (pos-visible-in-window-p (point-max) window))
  970.               nil        ; assume that the user will see it...
  971.             (ding t 'ready)
  972.             (display-message
  973.              'progress
  974.              (format "Compilation process completed%s."
  975.                  (or estatus " successfully")
  976.                  )))
  977.               ;; Since the buffer and mode line will show that the
  978.               ;; process is dead, we can delete it now.  Otherwise it
  979.               ;; will stay around until M-x list-processes.
  980.               (delete-process proc)
  981.               ;; Force mode line redisplay soon.
  982.               (redraw-modeline))
  983.             (if (and opoint (< opoint omax))
  984.             (goto-char opoint))
  985.             (if compilation-finish-function
  986.             (funcall compilation-finish-function buffer msg)))
  987.         (set-buffer obuf))))
  988.       (setq compilation-in-progress (delq proc compilation-in-progress))
  989.       ))))
  990.  
  991. (defun compilation-filter (proc string)
  992.   "Process filter for compilation buffers.
  993. Just inserts the text, but uses `insert-before-markers'."
  994.   (if (buffer-name (process-buffer proc))
  995.       (save-excursion
  996.     (set-buffer (process-buffer proc))
  997.     (let ((buffer-read-only nil))
  998.       (save-excursion
  999.         (goto-char (process-mark proc))
  1000.         (insert-before-markers string)
  1001.         (run-hooks 'compilation-filter-hook)
  1002.         (set-marker (process-mark proc) (point)))))))
  1003.  
  1004. ;; Return the cdr of compilation-old-error-list for the error containing point.
  1005. (defun compile-error-at-point ()
  1006.   (compile-reinitialize-errors nil (point))
  1007.   (let ((errors compilation-old-error-list))
  1008.     (while (and errors
  1009.         (> (point) (car (car errors))))
  1010.       (setq errors (cdr errors)))
  1011.     errors))
  1012.  
  1013. (defsubst compilation-buffer-p (buffer)
  1014.   (save-excursion
  1015.     (set-buffer buffer)
  1016.     (or compilation-minor-mode (eq major-mode 'compilation-mode))))
  1017.  
  1018. (defun compilation-next-error (n)
  1019.   "Move point to the next error in the compilation buffer.
  1020. Does NOT find the source line like \\[next-error]."
  1021.   (interactive "p")
  1022.   (or (compilation-buffer-p (current-buffer))
  1023.       (error "Not in a compilation buffer."))
  1024.   (setq compilation-last-buffer (current-buffer))
  1025.  
  1026.   (let ((errors (compile-error-at-point)))
  1027.  
  1028.     ;; Move to the error after the one containing point.
  1029.     (goto-char (car (if (< n 0)
  1030.             (let ((i 0)
  1031.                   (e compilation-old-error-list))
  1032.               ;; See how many cdrs away ERRORS is from the start.
  1033.               (while (not (eq e errors))
  1034.                 (setq i (1+ i)
  1035.                   e (cdr e)))
  1036.               (if (> (- n) i)
  1037.                   (error "Moved back past first error")
  1038.                 (nth (+ i n) compilation-old-error-list)))
  1039.               (let ((compilation-error-list (cdr errors)))
  1040.             (compile-reinitialize-errors nil nil n)
  1041.             (if compilation-error-list
  1042.                 (nth (1- n) compilation-error-list)
  1043.               (error "Moved past last error"))))))))
  1044.  
  1045. (defun compilation-previous-error (n)
  1046.   "Move point to the previous error in the compilation buffer.
  1047. Does NOT find the source line like \\[next-error]."
  1048.   (interactive "p")
  1049.   (compilation-next-error (- n)))
  1050.  
  1051.  
  1052. ;; Given an elt of `compilation-error-list', return an object representing
  1053. ;; the referenced file which is equal to (but not necessarily eq to) what
  1054. ;; this function would return for another error in the same file.
  1055. (defsubst compilation-error-filedata (data)
  1056.   (setq data (cdr data))
  1057.   (if (markerp data)
  1058.       (marker-buffer data)
  1059.     (car data)))
  1060.  
  1061. ;; Return a string describing a value from compilation-error-filedata.
  1062. ;; This value is not necessarily useful as a file name, but should be
  1063. ;; indicative to the user of what file's errors are being referred to.
  1064. (defsubst compilation-error-filedata-file-name (filedata)
  1065.   (if (bufferp filedata)
  1066.       (buffer-file-name filedata)
  1067.     (car filedata)))
  1068.  
  1069. (defun compilation-next-file (n)
  1070.   "Move point to the next error for a different file than the current one."
  1071.   (interactive "p")
  1072.   (or (compilation-buffer-p (current-buffer))
  1073.       (error "Not in a compilation buffer."))
  1074.   (setq compilation-last-buffer (current-buffer))
  1075.  
  1076.   (let ((reversed (< n 0))
  1077.     errors filedata)
  1078.  
  1079.     (if (not reversed)
  1080.     (setq errors (or (compile-error-at-point)
  1081.              (error "Moved past last error")))
  1082.  
  1083.       ;; Get a reversed list of the errors up through the one containing point.
  1084.       (compile-reinitialize-errors nil (point))
  1085.       (setq errors (reverse compilation-old-error-list)
  1086.         n (- n))
  1087.  
  1088.       ;; Ignore errors after point.  (car ERRORS) will be the error
  1089.       ;; containing point, (cadr ERRORS) the one before it.
  1090.       (while (and errors
  1091.           (< (point) (car (car errors))))
  1092.     (setq errors (cdr errors))))
  1093.  
  1094.     (while (> n 0)
  1095.       (setq filedata (compilation-error-filedata (car errors)))
  1096.  
  1097.       ;; Skip past the following errors for this file.
  1098.       (while (equal filedata
  1099.             (compilation-error-filedata
  1100.              (car (or errors
  1101.                   (if reversed
  1102.                   (error "%s the first erring file"
  1103.                      (compilation-error-filedata-file-name
  1104.                       filedata))
  1105.                 (let ((compilation-error-list nil))
  1106.                   ;; Parse some more.
  1107.                   (compile-reinitialize-errors nil nil 2)
  1108.                   (setq errors compilation-error-list)))
  1109.                   (error "%s is the last erring file" 
  1110.                      (compilation-error-filedata-file-name
  1111.                       filedata))))))
  1112.     (setq errors (cdr errors)))
  1113.  
  1114.       (setq n (1- n)))
  1115.  
  1116.     ;; Move to the following error.
  1117.     (goto-char (car (car (or errors
  1118.                  (if reversed
  1119.                  (error "This is the first erring file")
  1120.                    (let ((compilation-error-list nil))
  1121.                  ;; Parse the last one.
  1122.                  (compile-reinitialize-errors nil nil 1)
  1123.                  compilation-error-list))))))))
  1124.  
  1125. (defun compilation-previous-file (n)
  1126.   "Move point to the previous error for a different file than the current one."
  1127.   (interactive "p")
  1128.   (compilation-next-file (- n)))
  1129.  
  1130.  
  1131. (defun kill-compilation ()
  1132.   "Kill the process made by the \\[compile] command."
  1133.   (interactive)
  1134.   (let ((buffer (compilation-find-buffer)))
  1135.     (if (get-buffer-process buffer)
  1136.     (interrupt-process (get-buffer-process buffer))
  1137.       (error "The compilation process is not running."))))
  1138.  
  1139.  
  1140. ;; Parse any new errors in the compilation buffer,
  1141. ;; or reparse from the beginning if the user has asked for that.
  1142. (defun compile-reinitialize-errors (reparse
  1143.                                     &optional limit-search find-at-least)
  1144.   (save-excursion
  1145.     ;; XEmacs change: Below we made a change to possibly change the
  1146.     ;; selected window.  If we don't save and restore the old window
  1147.     ;; then if we get an error such as 'no more errors' we'll end up
  1148.     ;; in the compilation buffer.
  1149.     (save-window-excursion
  1150.       (set-buffer compilation-last-buffer)
  1151.       ;; If we are out of errors, or if user says "reparse",
  1152.       ;; discard the info we have, to force reparsing.
  1153.       (if (or (eq compilation-error-list t)
  1154.           reparse)
  1155.       (compilation-forget-errors))
  1156.       (if (and compilation-error-list
  1157.            (or (not limit-search)
  1158.          (> compilation-parsing-end limit-search))
  1159.          (or (not find-at-least)
  1160.          (>= (length compilation-error-list) find-at-least)))
  1161.     ;; Since compilation-error-list is non-nil, it points to a specific
  1162.     ;; error the user wanted.  So don't move it around.
  1163.     nil
  1164.  
  1165.       ;; XEmacs change: if the compilation buffer is already visible
  1166.       ;; in a window, use that instead of thrashing the display.
  1167.       (let ((w (get-buffer-window compilation-last-buffer)))
  1168.     (if w
  1169.         (select-window w)
  1170.       (switch-to-buffer compilation-last-buffer)))
  1171.  
  1172.       ;; This was here for a long time (before my rewrite); why? --roland
  1173.       ;;(switch-to-buffer compilation-last-buffer)
  1174.       (set-buffer-modified-p nil)
  1175.       (if (< compilation-parsing-end (point-max))
  1176.       ;; compilation-error-list might be non-nil if we have a non-nil
  1177.       ;; LIMIT-SEARCH or FIND-AT-LEAST arg.  In that case its value
  1178.       ;; records the current position in the error list, and we must
  1179.       ;; preserve that after reparsing.
  1180.       (let ((error-list-pos compilation-error-list))
  1181.         (funcall compilation-parse-errors-function
  1182.              limit-search
  1183.              (and find-at-least
  1184.               ;; We only need enough new parsed errors to reach
  1185.               ;; FIND-AT-LEAST errors past the current
  1186.               ;; position.
  1187.               (- find-at-least (length compilation-error-list))))
  1188.         ;; Remember the entire list for compilation-forget-errors.  If
  1189.         ;; this is an incremental parse, append to previous list.  If
  1190.         ;; we are parsing anew, compilation-forget-errors cleared
  1191.         ;; compilation-old-error-list above.
  1192.         (setq compilation-old-error-list
  1193.           (nconc compilation-old-error-list compilation-error-list))
  1194.         (if error-list-pos
  1195.         ;; We started in the middle of an existing list of parsed
  1196.         ;; errors before parsing more; restore that position.
  1197.         (setq compilation-error-list error-list-pos))
  1198.         ))))))
  1199.  
  1200. ;; XEmacs addition
  1201. ;; FSF has added this by 19.34, but it is highly complex, why? -sb
  1202. (defun compile-mouse-goto-error (event)
  1203.   "Visit the source for the error under the mouse.
  1204. Use this command in a compilation log buffer."
  1205.   (interactive "e")
  1206.   (mouse-set-point event)
  1207.   (beginning-of-line)
  1208.   (compile-goto-error))
  1209.  
  1210. (defun compile-goto-error (&optional argp)
  1211.   "Visit the source for the error message point is on.
  1212. Use this command in a compilation log buffer.  Sets the mark at point there.
  1213. \\[universal-argument] as a prefix arg means to reparse the buffer's error messages first;
  1214. other kinds of prefix arguments are ignored."
  1215.   (interactive "P")
  1216.   (or (compilation-buffer-p (current-buffer))
  1217.       (error "Not in a compilation buffer."))
  1218.   (setq compilation-last-buffer (current-buffer))
  1219.   (compile-reinitialize-errors (consp argp) (point))
  1220.  
  1221.   ;; Move to bol; the marker for the error on this line will point there.
  1222.   (beginning-of-line)
  1223.  
  1224.   ;; Move compilation-error-list to the elt of compilation-old-error-list
  1225.   ;; we want.
  1226.   (setq compilation-error-list compilation-old-error-list)
  1227.   (while (and compilation-error-list
  1228.           (> (point) (car (car compilation-error-list))))
  1229.     (setq compilation-error-list (cdr compilation-error-list)))
  1230.  
  1231.   ;; Move to another window, so that next-error's window changes
  1232.   ;; result in the desired setup.
  1233.   (or (one-window-p)
  1234.       (progn
  1235.     (other-window -1)
  1236.     ;; other-window changed the selected buffer,
  1237.     ;; but we didn't want to do that.
  1238.     (set-buffer compilation-last-buffer)))
  1239.  
  1240.   (push-mark)
  1241.   (next-error 1))
  1242.  
  1243. ;; XEmacs addition
  1244. (defun compile-mouse-maybe-goto-error (event &optional click-count)
  1245.   (interactive "e")
  1246.   (if (equal (event-button event) 2)
  1247.       (let ((buffer (current-buffer))
  1248.         (point (point))
  1249.         (config (current-window-configuration)))
  1250.     (condition-case nil
  1251.         (progn
  1252.           (compile-mouse-goto-error event)
  1253.           t)
  1254.       (error
  1255.        (set-window-configuration config)
  1256.        (set-buffer buffer)
  1257.        (goto-char point)
  1258.        nil)))))
  1259.  
  1260. ;; Return a compilation buffer.
  1261. ;; If the current buffer is a compilation buffer, return it.
  1262. ;; If compilation-last-buffer is set to a live buffer, use that.
  1263. ;; Otherwise, look for a compilation buffer and signal an error
  1264. ;; if there are none.
  1265. (defun compilation-find-buffer (&optional other-buffer)
  1266.   (if (and (not other-buffer)
  1267.        (compilation-buffer-p (current-buffer)))
  1268.       ;; The current buffer is a compilation buffer.
  1269.       (current-buffer)
  1270.     (if (and compilation-last-buffer (buffer-name compilation-last-buffer)
  1271.          (compilation-buffer-p compilation-last-buffer)
  1272.          (or (not other-buffer) (not (eq compilation-last-buffer
  1273.                          (current-buffer)))))
  1274.     compilation-last-buffer
  1275.       (let ((buffers (buffer-list)))
  1276.     (while (and buffers (or (not (compilation-buffer-p (car buffers)))
  1277.                 (and other-buffer
  1278.                      (eq (car buffers) (current-buffer)))))
  1279.       (setq buffers (cdr buffers)))
  1280.     (if buffers
  1281.         (car buffers)
  1282.       (or (and other-buffer
  1283.            (compilation-buffer-p (current-buffer))
  1284.            ;; The current buffer is a compilation buffer.
  1285.            (progn
  1286.              (if other-buffer
  1287.              (message "This is the only compilation buffer."))
  1288.              (current-buffer)))
  1289.           (error "No compilation started!")))))))
  1290.  
  1291. ;;;###autoload
  1292. (defun next-error (&optional argp)
  1293.   "Visit next compilation error message and corresponding source code.
  1294. This operates on the output from the \\[compile] command.
  1295. If all preparsed error messages have been processed,
  1296. the error message buffer is checked for new ones.
  1297.  
  1298. A prefix arg specifies how many error messages to move;
  1299. negative means move back to previous error messages.
  1300. Just C-u as a prefix means reparse the error message buffer
  1301. and start at the first error.
  1302.  
  1303. \\[next-error] normally applies to the most recent compilation started,
  1304. but as long as you are in the middle of parsing errors from one compilation
  1305. output buffer, you stay with that compilation output buffer.
  1306.  
  1307. Use \\[next-error] in a compilation output buffer to switch to
  1308. processing errors from that compilation.
  1309.  
  1310. See variables `compilation-parse-errors-function' and
  1311. \`compilation-error-regexp-alist' for customization ideas."
  1312.   (interactive "P")
  1313.   (setq compilation-last-buffer (compilation-find-buffer))
  1314.   (compilation-goto-locus (compilation-next-error-locus
  1315.                ;; We want to pass a number here only if
  1316.                ;; we got a numeric prefix arg, not just C-u.
  1317.                (and (not (consp argp))
  1318.                 (prefix-numeric-value argp))
  1319.                (consp argp))))
  1320. ;;;###autoload (define-key ctl-x-map "`" 'next-error)
  1321.  
  1322. ;; XEmacs change
  1323. ;;;###autoload
  1324. (defun previous-error (&optional argp)
  1325.   "Visit previous compilation error message and corresponding source code.
  1326. This operates on the output from the \\[compile] command."
  1327.   (interactive "P")
  1328.   (next-error (cond ((null argp) -1)
  1329.             ((numberp argp) (- argp))
  1330.             (t argp))))
  1331.  
  1332. ;;;###autoload
  1333. (defun first-error ()
  1334.   "Reparse the error message buffer and start at the first error
  1335. Visit corresponding source code.
  1336. This operates on the output from the \\[compile] command."
  1337.   (interactive)
  1338.   (next-error '(4)))
  1339.  
  1340. (defun compilation-next-error-locus (&optional move reparse silent)
  1341.   "Visit next compilation error and return locus in corresponding source code.
  1342. This operates on the output from the \\[compile] command.
  1343. If all preparsed error messages have been processed,
  1344. the error message buffer is checked for new ones.
  1345.  
  1346. Returns a cons (ERROR . SOURCE) of two markers: ERROR is a marker at the
  1347. location of the error message in the compilation buffer, and SOURCE is a
  1348. marker at the location in the source code indicated by the error message.
  1349.  
  1350. Optional first arg MOVE says how many error messages to move forwards (or
  1351. backwards, if negative); default is 1.  Optional second arg REPARSE, if
  1352. non-nil, says to reparse the error message buffer and reset to the first
  1353. error (plus MOVE - 1).  If optional third argument SILENT is non-nil, return 
  1354. nil instead of raising an error if there are no more errors.
  1355.  
  1356. The current buffer should be the desired compilation output buffer."
  1357.   (or move (setq move 1))
  1358.   (compile-reinitialize-errors reparse nil (and (not reparse)
  1359.                         (if (< move 1) 0 (1- move))))
  1360.   (let (next-errors next-error)
  1361.     (catch 'no-next-error
  1362.       (save-excursion
  1363.     (set-buffer compilation-last-buffer)
  1364.     ;; compilation-error-list points to the "current" error.
  1365.     (setq next-errors 
  1366.           (if (> move 0)
  1367.           (nthcdr (1- move)
  1368.               compilation-error-list)
  1369.                 ;; Zero or negative arg; we need to move back in the list.
  1370.                 (let ((n (1- move))
  1371.                       (i 0)
  1372.                       (e compilation-old-error-list))
  1373.                   ;; See how many cdrs away the current error is from the start.
  1374.                   (while (not (eq e compilation-error-list))
  1375.                     (setq i (1+ i)
  1376.                           e (cdr e)))
  1377.                   (if (> (- n) i)
  1378.                       (error "Moved back past first error")
  1379.             (nthcdr (+ i n) compilation-old-error-list))))
  1380.           next-error (car next-errors))
  1381.     (while
  1382.         (if (null next-error)
  1383.         (progn
  1384.           (and move (/= move 1)
  1385.                (error (if (> move 0)
  1386.                   "Moved past last error"
  1387.                                 "Moved back past first error")))
  1388. ;; Forget existing error messages if compilation has finished.
  1389. ;;; XEmacs change by Barry Warsaw.
  1390. ;;; Without this, if you get a "no more errors" error, then you can't do
  1391. ;;; previous-error or goto-error until you kill the buffer.
  1392. ;          (if (not (and (get-buffer-process (current-buffer))
  1393. ;                (eq (process-status
  1394. ;                     (get-buffer-process
  1395. ;                      (current-buffer)))
  1396. ;                    'run)))
  1397. ;              (compilation-forget-errors))
  1398.           (if silent
  1399.               (throw 'no-next-error nil)
  1400.             (error (concat compilation-error-message
  1401.                    (and (get-buffer-process (current-buffer))
  1402.                     (eq (process-status
  1403.                          (get-buffer-process
  1404.                                  (current-buffer)))
  1405.                         'run)
  1406.                     " yet")))))
  1407.           (setq compilation-error-list (cdr next-errors))
  1408.           (if (null (cdr next-error))
  1409.           ;; This error is boring.  Go to the next.
  1410.           t
  1411.         (or (markerp (cdr next-error))
  1412.             ;; This error has a filename/lineno pair.
  1413.             ;; Find the file and turn it into a marker.
  1414.             (let* ((fileinfo (car (cdr next-error)))
  1415.                (cbuf (current-buffer)) ;XEmacs addition
  1416.                (buffer (apply 'compilation-find-file
  1417.                       (car next-error) fileinfo)))
  1418.               (if (null buffer)
  1419.               ;; We can't find this error's file.
  1420.               ;; Remove all errors in the same file.
  1421.               (progn
  1422.                 (setq next-errors compilation-old-error-list)
  1423.                 (while next-errors
  1424.                   (and (consp (cdr (car next-errors)))
  1425.                    (equal (car (cdr (car next-errors)))
  1426.                       fileinfo)
  1427.                    (progn
  1428.                      (set-marker (car (car next-errors)) nil)
  1429.                      (setcdr (car next-errors) nil)))
  1430.                   (setq next-errors (cdr next-errors)))
  1431.                 ;; Look for the next error.
  1432.                 t)
  1433.             ;; We found the file.  Get a marker for this error.
  1434.             ;; compilation-old-error-list is a buffer-local
  1435.             ;; variable, so we must be careful to extract its value
  1436.             ;; before switching to the source file buffer.
  1437.             (let ((errors compilation-old-error-list)
  1438.                   (last-line (nth 1 (cdr next-error)))
  1439.                   (column (nth 2 (cdr next-error))))
  1440.               (set-buffer buffer)
  1441.               (save-excursion
  1442.                 (save-restriction
  1443.                   (widen)
  1444.                   (goto-line last-line)
  1445.                   (if (and column (> column 0))
  1446.                   ;; Columns in error msgs are 1-origin.
  1447.                   (move-to-column (1- column))
  1448.                 (beginning-of-line))
  1449.                   (setcdr next-error (point-marker))
  1450.                   ;; Make all the other error messages referring
  1451.                   ;; to the same file have markers into the buffer.
  1452.                   (while errors
  1453.                 (and (consp (cdr (car errors)))
  1454.                      (equal (car (cdr (car errors))) fileinfo)
  1455.                      (let* ((this (nth 1 (cdr (car errors))))
  1456.                         (column (nth 2 (cdr (car errors))))
  1457.                         (lines (- this last-line)))
  1458.                        (if (eq selective-display t)
  1459.                        ;; When selective-display is t,
  1460.                        ;; each C-m is a line boundary,
  1461.                        ;; as well as each newline.
  1462.                        (if (< lines 0)
  1463.                            (re-search-backward "[\n\C-m]"
  1464.                                    nil 'end
  1465.                                    (- lines))
  1466.                                              (re-search-forward "[\n\C-m]"
  1467.                                                                 nil 'end
  1468.                                                                 lines))
  1469.                                          (forward-line lines))
  1470.                        (if (and column (> column 1))
  1471.                        (move-to-column (1- column))
  1472.                      (beginning-of-line))
  1473.                        (setq last-line this)
  1474.                        (setcdr (car errors) (point-marker))))
  1475.                 (setq errors (cdr errors)))))
  1476.               ;; XEmacs addition
  1477.               (set-buffer cbuf)))))
  1478.                 ;; If we didn't get a marker for this error, or this
  1479.                 ;; marker's buffer was killed, go on to the next one.
  1480.                 (or (not (markerp (cdr next-error)))
  1481.                     (not (marker-buffer (cdr next-error))))))
  1482.       (setq next-errors compilation-error-list
  1483.         next-error (car next-errors)))
  1484.  
  1485.     ;; XEmacs -- move this inside save-excursion
  1486.     ;; Skip over multiple error messages for the same source location,
  1487.     ;; so the next C-x ` won't go to an error in the same place.
  1488.     (while (and compilation-error-list
  1489.             (equal (cdr (car compilation-error-list))
  1490.                (cdr next-error)))
  1491.       (setq compilation-error-list (cdr compilation-error-list)))
  1492.     ))
  1493.  
  1494.     ;; XEmacs change: If a new window has to be displayed, select the other
  1495.     ;; window to avoid swapping the position of the compilation error buffer.
  1496.     (and next-error (get-buffer-window (marker-buffer (car next-error)))
  1497.          (progn
  1498.            (select-window (get-buffer-window (marker-buffer (car next-error))))
  1499.            (other-window -1)))
  1500.       
  1501.     ;; We now have a marker for the position of the error source code.
  1502.     ;; NEXT-ERROR is a cons (ERROR . SOURCE) of two markers.
  1503.     next-error))
  1504.  
  1505. (defun compilation-goto-locus (next-error)
  1506.   "Jump to an error locus returned by `compilation-next-error-locus'.
  1507. Takes one argument, a cons (ERROR . SOURCE) of two markers.
  1508. Selects a window with point at SOURCE, with another window displaying ERROR."
  1509. ;; XEmacs: this code is horrendous, and makes windows do all sorts of
  1510. ;; weird things when you're using separate frames for the compilation
  1511. ;; and source buffer.
  1512. ;  (if (and (window-dedicated-p (selected-window))
  1513. ;       (eq (selected-window) (frame-root-window)))
  1514. ;      (switch-to-buffer-other-frame (marker-buffer (cdr next-error)))
  1515. ;    (switch-to-buffer (marker-buffer (cdr next-error))))
  1516. ;  (goto-char (cdr next-error))
  1517. ;  ;; If narrowing got in the way of
  1518. ;  ;; going to the right place, widen.
  1519. ;  (or (= (point) (marker-position (cdr next-error)))
  1520. ;      (progn
  1521. ;        (widen)
  1522. ;        (goto-char (cdr next-error))))
  1523. ;
  1524. ;  ;; Show compilation buffer in other window, scrolled to this error.
  1525. ;  (let* ((pop-up-windows t)
  1526. ;     (w (or (get-buffer-window (marker-buffer (car next-error)) 'visible)
  1527. ;        (display-buffer (marker-buffer (car next-error))))))
  1528. ;    (set-window-point w (car next-error))
  1529. ;    (set-window-start w (car next-error))
  1530. ;    (compilation-set-window-height w)))
  1531.  
  1532.   (let* ((pop-up-windows t)
  1533.      (compilation-buffer (marker-buffer (car next-error)))
  1534.      (source-buffer (marker-buffer (cdr next-error)))
  1535.      ;; make sure compilation buffer is visible ...
  1536.      (compilation-window
  1537.      ;; Use an existing window if it is in a visible frame.
  1538.       (or (get-buffer-window compilation-buffer 'visible)
  1539.           ;; Pop up a window.
  1540.           (display-buffer compilation-buffer))))
  1541.  
  1542.     ;; now, make the compilation buffer **STAY WHERE IT IS** and
  1543.     ;; make sure the source buffer is visible
  1544.  
  1545.     (select-window compilation-window)
  1546.     (pop-to-buffer source-buffer)
  1547.  
  1548.     ;; now put things aright in the compilation window.
  1549.     (set-window-point compilation-window (car next-error))
  1550.     (set-window-start compilation-window (car next-error))
  1551.     (compilation-set-window-height compilation-window)
  1552.  
  1553.     ;; now put things aright in the source window.
  1554.  
  1555.     (set-buffer source-buffer)
  1556.     (goto-char (cdr next-error))
  1557.     ;; If narrowing got in the way of
  1558.     ;; going to the right place, widen.
  1559.     (or (= (point) (marker-position (cdr next-error)))
  1560.     (progn
  1561.       (widen)
  1562.       (goto-char (cdr next-error))))))
  1563.     
  1564.  
  1565. ;; Find a buffer for file FILENAME.
  1566. ;; Search the directories in compilation-search-path.
  1567. ;; A nil in compilation-search-path means to try the
  1568. ;; current directory, which is passed in DIR.
  1569. ;; If FILENAME is not found at all, ask the user where to find it.
  1570. ;; Pop up the buffer containing MARKER and scroll to MARKER if we ask the user.
  1571. (defun compilation-find-file (marker filename dir &rest formats)
  1572.   (or formats (setq formats '("%s")))
  1573.   (let ((dirs compilation-search-path)
  1574.     buffer thisdir fmts name)
  1575.     (if (file-name-absolute-p filename)
  1576.     ;; The file name is absolute.  Use its explicit directory as
  1577.     ;; the first in the search path, and strip it from FILENAME.
  1578.     (setq filename (abbreviate-file-name (expand-file-name filename))
  1579.           dirs (cons (file-name-directory filename) dirs)
  1580.           filename (file-name-nondirectory filename)))
  1581.     ;; Now search the path.
  1582.     (while (and dirs (null buffer))
  1583.       (setq thisdir (or (car dirs) dir)
  1584.         fmts formats)
  1585.       ;; For each directory, try each format string.
  1586.       (while (and fmts (null buffer))
  1587.     (setq name (expand-file-name (format (car fmts) filename) thisdir)
  1588.           buffer (and (file-exists-p name)
  1589.               (find-file-noselect name))
  1590.           fmts (cdr fmts)))
  1591.       (setq dirs (cdr dirs)))
  1592.     (or buffer
  1593.     ;; The file doesn't exist.
  1594.     ;; Ask the user where to find it.
  1595.     ;; If he hits C-g, then the next time he does
  1596.     ;; next-error, he'll skip past it.
  1597.     (let* ((pop-up-windows t)
  1598.            (w (display-buffer (marker-buffer marker))))
  1599.       (set-window-point w marker)
  1600.       (set-window-start w marker)
  1601.       (let ((name (expand-file-name
  1602.                (read-file-name
  1603.             (format "Find this error in: (default %s) "
  1604.                 filename)
  1605.             dir filename t))))
  1606.         (if (file-directory-p name)
  1607.         (setq name (expand-file-name filename name)))
  1608.         (and (file-exists-p name)
  1609.          (find-file-noselect name)))))))
  1610.  
  1611. ;; Set compilation-error-list to nil, and unchain the markers that point to the
  1612. ;; error messages and their text, so that they no longer slow down gap motion.
  1613. ;; This would happen anyway at the next garbage collection, but it is better to
  1614. ;; do it right away.
  1615. (defun compilation-forget-errors ()
  1616.   (while compilation-old-error-list
  1617.     (let ((next-error (car compilation-old-error-list)))
  1618.       (set-marker (car next-error) nil)
  1619.       (if (markerp (cdr next-error))
  1620.       (set-marker (cdr next-error) nil)))
  1621.     (setq compilation-old-error-list (cdr compilation-old-error-list)))
  1622.   (setq compilation-error-list nil
  1623.     compilation-directory-stack nil
  1624.     compilation-parsing-end 1))
  1625.  
  1626.  
  1627. (defun count-regexp-groupings (regexp)
  1628.   "Return the number of \\( ... \\) groupings in REGEXP (a string)."
  1629.   (let ((groupings 0)
  1630.     (len (length regexp))
  1631.     (i 0)
  1632.     c)
  1633.     (while (< i len)
  1634.       (setq c (aref regexp i)
  1635.         i (1+ i))
  1636.       (cond ((= c ?\[)
  1637.          ;; Find the end of this [...].
  1638.          (while (and (< i len)
  1639.              (not (= (aref regexp i) ?\])))
  1640.            (setq i (1+ i))))
  1641.         ((= c ?\\)
  1642.          (if (< i len)
  1643.          (progn
  1644.            (setq c (aref regexp i)
  1645.              i (1+ i))
  1646.            (if (= c ?\))
  1647.                ;; We found the end of a grouping,
  1648.                ;; so bump our counter.
  1649.                (setq groupings (1+ groupings))))))))
  1650.     groupings))
  1651.  
  1652. (defun compilation-parse-errors (limit-search find-at-least)
  1653.   "Parse the current buffer as grep, cc or lint error messages.
  1654. See variable `compilation-parse-errors-function' for the interface it uses."
  1655.   (setq compilation-error-list nil)
  1656.   (display-message 'progress "Parsing error messages...")
  1657.   (let (;;text-buffer -- unused
  1658.     orig orig-expanded parent-expanded
  1659.     regexp enter-group leave-group error-group
  1660.     alist subexpr error-regexp-groups
  1661.     (found-desired nil)
  1662.     (compilation-num-errors-found 0)
  1663.         (message-freq (max 1 (/ (count-lines (point-min) (point-max)) 50))))
  1664.  
  1665.     ;; Don't reparse messages already seen at last parse.
  1666.     (goto-char compilation-parsing-end)
  1667.     ;; Don't parse the first two lines as error messages.
  1668.     ;; This matters for grep.
  1669.     (if (bobp)
  1670.     (progn
  1671.       (forward-line 2)
  1672.       ;; Move back so point is before the newline.
  1673.       ;; This matters because some error regexps use \n instead of ^
  1674.       ;; to be faster.
  1675.       (forward-char -1)))
  1676.  
  1677.     ;; Compile all the regexps we want to search for into one.
  1678.     (setq regexp (concat "\\(" compilation-enter-directory-regexp "\\)\\|"
  1679.              "\\(" compilation-leave-directory-regexp "\\)\\|"
  1680.              "\\(" (mapconcat (function
  1681.                        (lambda (elt)
  1682.                          (concat "\\(" (car elt) "\\)")))
  1683.                       compilation-error-regexp-alist
  1684.                       "\\|") "\\)"))
  1685.  
  1686.     ;; Find out how many \(...\) groupings are in each of the regexps, and set
  1687.     ;; *-GROUP to the grouping containing each constituent regexp (whose
  1688.     ;; subgroups will come immediately thereafter) of the big regexp we have
  1689.     ;; just constructed.
  1690.     (setq enter-group 1
  1691.       leave-group (+ enter-group
  1692.              (count-regexp-groupings
  1693.               compilation-enter-directory-regexp)
  1694.              1)
  1695.       error-group (+ leave-group
  1696.              (count-regexp-groupings
  1697.               compilation-leave-directory-regexp)
  1698.              1))
  1699.  
  1700.     ;; Compile an alist (IDX FILE LINE [COL]), where IDX is the number of
  1701.     ;; the subexpression for an entire error-regexp, and FILE and LINE (and
  1702.     ;; possibly COL) are the numbers for the subexpressions giving the file
  1703.     ;; name and line number (and possibly column number).
  1704.     (setq alist (or compilation-error-regexp-alist
  1705.             (error "compilation-error-regexp-alist is empty!"))
  1706.       subexpr (1+ error-group))
  1707.     (while alist
  1708.       (setq error-regexp-groups
  1709.         (cons (list subexpr
  1710.             (+ subexpr (nth 1 (car alist)))
  1711.             (+ subexpr (nth 2 (car alist)))
  1712.             ;;#### This is buggy in FSFmacs
  1713.             (let ((col (nth 3 (car alist))))
  1714.               (and col
  1715.                    (+ subexpr col))))
  1716.           error-regexp-groups))
  1717.       (setq subexpr (+ subexpr 1 (count-regexp-groupings (car (car alist)))))
  1718.       (setq alist (cdr alist)))
  1719.  
  1720.     ;; Set up now the expanded, abbreviated directory variables
  1721.     ;; that compile-abbreviate-directory will need, so we can
  1722.     ;; compute them just once here.
  1723.     (setq orig (abbreviate-file-name default-directory)
  1724.       orig-expanded (abbreviate-file-name
  1725.              (file-truename default-directory))
  1726.       parent-expanded (abbreviate-file-name
  1727.                (expand-file-name "../" orig-expanded)))
  1728.  
  1729.     (while (and (not found-desired)
  1730.         ;; We don't just pass LIMIT-SEARCH to re-search-forward
  1731.         ;; because we want to find matches containing LIMIT-SEARCH
  1732.         ;; but which extend past it.
  1733.         ;; Instead of using re-search-forward,
  1734.         ;; we use this loop which tries only at each line.
  1735.         (progn
  1736.           (while (and (not (eobp))
  1737.                   (not (looking-at regexp)))
  1738.             (forward-line 1))
  1739.           (not (eobp))))
  1740.  
  1741.       ;; Move to the end of the match we just found.
  1742.       (goto-char (match-end 0))
  1743.  
  1744.       ;; Figure out which constituent regexp matched.
  1745.       (cond ((match-beginning enter-group)
  1746.          ;; The match was the enter-directory regexp.
  1747.          (let ((dir
  1748.             (file-name-as-directory
  1749.              (expand-file-name
  1750.               (buffer-substring (match-beginning (+ enter-group 1))
  1751.                     (match-end (+ enter-group 1)))))))
  1752.            ;; The directory name in the "entering" message
  1753.            ;; is a truename.  Try to convert it to a form
  1754.            ;; like what the user typed in.
  1755.            (setq dir
  1756.              (compile-abbreviate-directory dir orig orig-expanded
  1757.                            parent-expanded))
  1758.            (setq compilation-directory-stack
  1759.              (cons dir compilation-directory-stack))
  1760.            (and (file-directory-p dir)
  1761.             (setq default-directory dir)))
  1762.  
  1763.          (and limit-search (>= (point) limit-search)
  1764.           ;; The user wanted a specific error, and we're past it.
  1765.           ;; We do this check here (and in the leave-group case)
  1766.           ;; rather than at the end of the loop because if the last
  1767.           ;; thing seen is an error message, we must carefully
  1768.           ;; discard the last error when it is the first in a new
  1769.           ;; file (see below in the error-group case).
  1770.           (setq found-desired t)))
  1771.         
  1772.         ((match-beginning leave-group)
  1773.          ;; The match was the leave-directory regexp.
  1774.          (let ((beg (match-beginning (+ leave-group 1)))
  1775.            (stack compilation-directory-stack))
  1776.            (if beg
  1777.            (let ((dir
  1778.               (file-name-as-directory
  1779.                (expand-file-name
  1780.                 (buffer-substring beg
  1781.                           (match-end (+ leave-group
  1782.                                 1)))))))
  1783.              ;; The directory name in the "leaving" message
  1784.              ;; is a truename.  Try to convert it to a form
  1785.              ;; like what the user typed in.
  1786.              (setq dir
  1787.                (compile-abbreviate-directory dir orig orig-expanded
  1788.                              parent-expanded))
  1789.              (while (and stack
  1790.                  (not (string-equal (car stack) dir)))
  1791.                (setq stack (cdr stack)))))
  1792.            (setq compilation-directory-stack (cdr stack))
  1793.            (setq stack (car compilation-directory-stack))
  1794.            (if stack
  1795.            (setq default-directory stack))
  1796.            )
  1797.  
  1798.          (and limit-search (>= (point) limit-search)
  1799.           ;; The user wanted a specific error, and we're past it.
  1800.           ;; We do this check here (and in the enter-group case)
  1801.           ;; rather than at the end of the loop because if the last
  1802.           ;; thing seen is an error message, we must carefully
  1803.           ;; discard the last error when it is the first in a new
  1804.           ;; file (see below in the error-group case).
  1805.           (setq found-desired t)))
  1806.         
  1807.         ((match-beginning error-group)
  1808.          ;; The match was the composite error regexp.
  1809.          ;; Find out which individual regexp matched.
  1810.          (setq alist error-regexp-groups)
  1811.          (while (and alist
  1812.              (null (match-beginning (car (car alist)))))
  1813.            (setq alist (cdr alist)))
  1814.          (if alist
  1815.          (setq alist (car alist))
  1816.            (error "compilation-parse-errors: impossible regexp match!"))
  1817.          
  1818.          ;; Extract the file name and line number from the error message.
  1819.          (let ((beginning-of-match (match-beginning 0)) ;looking-at nukes
  1820.            (filename (buffer-substring (match-beginning (nth 1 alist))
  1821.                            (match-end (nth 1 alist))))
  1822.            (linenum (string-to-int
  1823.                  (buffer-substring
  1824.                   (match-beginning (nth 2 alist))
  1825.                   (match-end (nth 2 alist)))))
  1826.            (column (and (nth 3 alist)
  1827.                 (match-beginning (nth 3 alist))
  1828.                 (string-to-int
  1829.                  (buffer-substring
  1830.                   (match-beginning (nth 3 alist))
  1831.                   (match-end (nth 3 alist)))))))
  1832.  
  1833.            ;; Check for a comint-file-name-prefix and prepend it if
  1834.            ;; appropriate.  (This is very useful for
  1835.            ;; compilation-minor-mode in an rlogin-mode buffer.)
  1836.            (and (boundp 'comint-file-name-prefix)
  1837.             ;; If the file name is relative, default-directory will
  1838.             ;; already contain the comint-file-name-prefix (done by
  1839.             ;; compile-abbreviate-directory).
  1840.             (file-name-absolute-p filename)
  1841.             (setq filename (concat comint-file-name-prefix filename)))
  1842.  
  1843.            ;; Some compilers (e.g. Sun's java compiler, reportedly)
  1844.            ;; produce bogus file names like "./bar//foo.c" for the file
  1845.            ;; "bar/foo.c"; expand-file-name will collapse these into
  1846.            ;; "/foo.c" and fail to find the appropriate file.  So we look
  1847.            ;; for doubled slashes in the file name and fix them up in the
  1848.            ;; buffer.
  1849.            (when (fboundp 'command-line-normalize-file-name)
  1850.          (setq filename (command-line-normalize-file-name filename)))
  1851.            (setq filename (cons filename (cons default-directory
  1852.                            (nthcdr 4 alist))))
  1853.                      
  1854.  
  1855.            ;; Locate the erring file and line.
  1856.            ;; Cons a new elt onto compilation-error-list,
  1857.            ;; giving a marker for the current compilation buffer
  1858.            ;; location, and the file and line number of the error.
  1859.            (save-excursion
  1860.          ;; Save as the start of the error the beginning of the
  1861.          ;; line containing the match unless the match starts at a
  1862.          ;; newline, in which case the beginning of the next line.
  1863.          (goto-char beginning-of-match)
  1864.          (forward-line (if (eolp) 1 0))
  1865.          (let ((this (cons (point-marker)
  1866.                    (list filename linenum column))))
  1867.            ;; Don't add the same source line more than once.
  1868.            (if (equal (cdr this) (cdr (car compilation-error-list)))
  1869.                nil
  1870.              (setq compilation-error-list
  1871.                (cons this
  1872.                  compilation-error-list))
  1873.              (setq compilation-num-errors-found
  1874.                (1+ compilation-num-errors-found)))))
  1875.            (and (or (and find-at-least (> compilation-num-errors-found
  1876.                           find-at-least))
  1877.             (and limit-search (>= (point) limit-search)))
  1878.             ;; We have found as many new errors as the user wants,
  1879.             ;; or past the buffer position he indicated.  We
  1880.             ;; continue to parse until we have seen all the
  1881.             ;; consecutive errors in the same file, so the error
  1882.                     ;; positions will be recorded as markers in this buffer
  1883.                     ;; that might change.
  1884.             (cdr compilation-error-list) ; Must check at least two.
  1885.             (not (equal (car (cdr (nth 0 compilation-error-list)))
  1886.                 (car (cdr (nth 1 compilation-error-list)))))
  1887.             (progn
  1888.               ;; Discard the error just parsed, so that the next
  1889.               ;; parsing run can get it and the following errors in
  1890.               ;; the same file all at once.  If we didn't do this, we
  1891.               ;; would have the same problem we are trying to avoid
  1892.               ;; with the test above, just delayed until the next run!
  1893.               (setq compilation-error-list
  1894.                 (cdr compilation-error-list))
  1895.               (goto-char beginning-of-match)
  1896.               (setq found-desired t)))
  1897.            )
  1898.          )
  1899.         (t
  1900.          (error "compilation-parse-errors: known groups didn't match!")))
  1901.  
  1902.       (when (= (% compilation-num-errors-found message-freq) 0)
  1903.         (display-message
  1904.      'progress
  1905.      (format "Parsing error messages...%d (%.0f%% of buffer)"
  1906.                  compilation-num-errors-found
  1907.                  ;; Use floating-point because (* 100 (point)) frequently
  1908.                  ;; exceeds the range of Emacs Lisp integers.
  1909.                  (/ (* 100.0 (point)) (point-max)))))
  1910.  
  1911. ;;; This is broken - it foils the logic above which is supposed to ensure
  1912. ;;; that all errors for the current file are found before we quit. 
  1913. ;      (and limit-search (>= (point) limit-search)
  1914. ;       ;; The user wanted a specific error, and we're past it.
  1915. ;       (setq found-desired t))
  1916.       )
  1917.     (setq compilation-parsing-end (if found-desired
  1918.                       (point)
  1919.                     ;; We have searched the whole buffer.
  1920.                     (point-max))))
  1921.   (setq compilation-error-list (nreverse compilation-error-list))
  1922.   (display-message 'progress "Parsing error messages...done"))
  1923.  
  1924. ;; If directory DIR is a subdir of ORIG or of ORIG's parent,
  1925. ;; return a relative name for it starting from ORIG or its parent.
  1926. ;; ORIG-EXPANDED is an expanded version of ORIG.
  1927. ;; PARENT-EXPANDED is an expanded version of ORIG's parent.
  1928. ;; Those two args could be computed here, but we run faster by
  1929. ;; having the caller compute them just once.
  1930. (defun compile-abbreviate-directory (dir orig orig-expanded parent-expanded)
  1931.   ;; Apply canonical abbreviations to DIR first thing.
  1932.   ;; Those abbreviations are already done in the other arguments passed.
  1933.   (setq dir (abbreviate-file-name dir))
  1934.  
  1935.   ;; Check for a comint-file-name-prefix and prepend it if appropriate.
  1936.   ;; (This is very useful for compilation-minor-mode in an rlogin-mode
  1937.   ;; buffer.)
  1938.   (if (boundp 'comint-file-name-prefix)
  1939.       (setq dir (concat comint-file-name-prefix dir)))
  1940.  
  1941.   (if (and (> (length dir) (length orig-expanded))
  1942.        (string= orig-expanded
  1943.             (substring dir 0 (length orig-expanded))))
  1944.       (setq dir
  1945.         (concat orig
  1946.             (substring dir (length orig-expanded)))))
  1947.   (if (and (> (length dir) (length parent-expanded))
  1948.        (string= parent-expanded
  1949.             (substring dir 0 (length parent-expanded))))
  1950.     (setq dir
  1951.       (concat (file-name-directory
  1952.            (directory-file-name orig))
  1953.           (substring dir (length parent-expanded)))))
  1954.   dir)
  1955.  
  1956. (provide 'compile)
  1957.  
  1958. ;;; compile.el ends here
  1959.